In previous chapter, we discussed how to train an object classifier using our own images. At the end, we got trained model and labels file (retrained_graph.pb, retrained_labels.txt).
In this chapter, We are going to load pre-trained classifer in our own Android app. Unfortunately, we can not use trained model in Android directly. We need to optimize it using a tool, namely "optimize_for_inference", provided by Tensorflow.
git clone https://github.com/tensorflow/tensorflow.git
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.listcurl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -sudo apt-get update && sudo apt-get install bazelsudo apt-get upgrade bazel
cd tensorflow./configure # We can choose all default settingsbazel build tensorflow/python/tools:optimize_for_inference # this process takes a while, be patient
Let's assume that our pre-trained model is <folder_path>/retrained_graph.pb. Then, we can use the following command to optimize the model and save it as <folder_path>/retrained_graph_android.pb
bazel-bin/tensorflow/python/tools/optimize_for_inference \--input=<folder_path>/retrained_graph.pb \--output=<folder_path>/retrained_graph_android.pb \--input_names=Mul \--output_names=final_result
If we import this project into Android Studio, compile and run, the demo will load a pre-trained classifier which can recognize 1000 classes.git clone https://github.com/Nilhcem/tensorflow-classifier-android.git
private static final int INPUT_SIZE = 299;private static final int IMAGE_MEAN = 128;private static final float IMAGE_STD = 128;private static final String INPUT_NAME = "Mul";private static final String OUTPUT_NAME = "final_result";
Next: 3. Drone_setup, Previous: 1. How to train an object classifier using our own images, Up: Index